package com.sisgh.jefdv1.util;

/**
 * String 工具类
 */
public class StringUtils extends com.mysql.cj.util.StringUtils {

    private static final String FORMAT_VALUE = "{}";
    private static final char HUMP_TAG = '_';

    /**
     * 驼峰转下划线
     * @param hump
     * @return
     */
    public static String camelToUnderline(final String hump) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hump.length(); i++) {
            char c = hump.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(HUMP_TAG);
                sb.append(Character.toLowerCase(c));
            }
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 下划线转驼峰（实际处理）
     * @param underline
     * @param sb
     * @return
     */
    private static String doUnderlineToCamel(final String underline, StringBuilder sb) {
        for (int i = 1; i < underline.length(); i++) {
            char c = underline.charAt(i);
            if (c == HUMP_TAG && ++i < underline.length()) {
                sb.append(Character.toUpperCase(underline.charAt(i)));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 下划线转小驼峰
     * @param underline
     * @return
     */
    public static String UnderlineToCamel(final String underline) {
        StringBuilder sb = new StringBuilder();
        sb.append(underline.charAt(0));
        return doUnderlineToCamel(underline, sb);
    }

    /**
     * 下划线转大驼峰
     * @param underline
     * @return
     */
    public static String UnderlineToUpperCamel(final String underline) {
        StringBuilder sb = new StringBuilder();
        sb.append(Character.toUpperCase(underline.charAt(0)));
        return doUnderlineToCamel(underline, sb);
    }

    /**
     * 构建驼峰
     * @param values
     * @return
     */
    public static String buildCamel(String... values) {
        if (values == null || values.length == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder(values[0]);
        for (int i = 1; i < values.length; i++) {
            sb.append(Character.toUpperCase(values[i].charAt(0)));
            for (int j = 1; j < values[i].length(); j++) {
                sb.append(values[i].charAt(j));
            }
        }

        return sb.toString();
    }

    /**
     * 转换成大驼峰
     * @param str
     * @return
     */
    public static String toUpperCamel(final String str) {
        if (isNullOrEmpty(str)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(Character.toUpperCase(str.charAt(0)));
        for (int i = 1; i < str.length(); i++) {
            sb.append(str.charAt(i));
        }
        return sb.toString();
    }

    public static String format(String format, Object... args) {
        return StringUtils.format(format, FORMAT_VALUE, args);
    }

    public static String format(String format, String replaceTag, Object... args) {
        if (isEmptyOrWhitespaceOnly(format)) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        int start = 0;
        for (Object arg : args) {
            int end = format.indexOf(replaceTag, start);

            if (end == -1) {
                return sb.toString();
            }

            if (arg != null) {
                sb.append(format, start, end).append(arg);
            }

            start = end + replaceTag.length();
        }

        return sb.toString();
    }
}
